package com.tyut.service.impl;

import com.tyut.mapper.CategoryMapper;
import com.tyut.pojo.Category;
import com.tyut.service.CategoryService;

import com.tyut.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
	@Autowired
	private CategoryMapper categoryMapper;
	@Override
	public void add(Category category) {
		Map<String, Object> map = ThreadLocalUtil.get();
		category.setCreateUser((Integer) map.get("id"));
		category.setUpdateTime(LocalDateTime.now());
		category.setCreateTime(LocalDateTime.now());
		categoryMapper.add(category);
	}

	@Override
	public List<Category> find() {
		Map<String,Object> cliam = ThreadLocalUtil.get();
		Integer id = (Integer) cliam.get("id");
		List<Category> category = categoryMapper.find(id);
		return category;
	}

	@Override
	public Category findById(Integer id) {
		Category category = categoryMapper.findById(id);
		return category;
	}

	@Override
	public void update(Category category) {
		category.setUpdateTime(LocalDateTime.now());
		categoryMapper.update(category);
	}

	@Override
	public void delete(Integer id) {
		categoryMapper.delete(id);
	}

	@Override
	public List<Category> findAll() {
		Map<String,Object> cliam = ThreadLocalUtil.get();
		Integer id = (Integer) cliam.get("id");
		List<Category> list = categoryMapper.findAll(id,0);
		return list;
	}
}
